﻿//修改颜色和字体
$(function () {
    show('colorDiv');
    window.form = $('#form001');
    bindValidate(ajaxSubmit, form);
    bindChooseColor();
    //设置背景图片
    $(document).on('click', '[setbg=""]', function () {
        var t = $(this).attr('t');
        var color = $(this).siblings('input[color]').val().replace('#', '');
        var url = "choosebgImg.aspx?siteId=" + siteId + "&t=" + t + "&color=" + color;
        var data2 = { title: txt_EditPage, width: ymWidth, height: ymHeight, maxBtn: true, minBtn: false, iframe: { src: url } };
        data2 = $.extend(ymdata, data2);
        ymPrompt.win(data2);
    });
});
//每次切换时重新绑定事件
function bindChooseColor() {
    $('input[color]').each(function (i, e) {
        $(this).bigColorpicker(function (el, color) {
            if (color === "") {
                color = "#FFFFFF";
            }
            $(el).val(color);
            $(el).siblings('div').css('backgroundColor', color);
        });
        $(this).on('blur', function () {
            var css = $(this).val();
            if (css === '') {
                css = '#FFFFFF';
            }
            $(this).siblings('div').css('backgroundColor', css);
        });
    });
    $('[choosecolor]').each(function (i, e) {
        $(this).bigColorpicker(function (el, color) {
            if (color == "") {
                color = "#FFFFFF";
            }
            $(el).siblings('input[color]').val(color);
            $(el).css('backgroundColor', color);
        });
    });
    $('#content [setDefault="color"]').on('click', function () {
        setDefault('color');
    });
    $('#content [setDefault="font"]').on('click', function () {
        setDefault('font');
    });
    //去除与默认相同的数据
    $('select').each(function (i, e) {
        var firstVal = $(this).find('option:eq(0)').val();
    });

    $('select').on('change', function () {
        var name = $(this).attr('name');
        var input = $(this).siblings('input');
        if ($(this).val() == '') {
            if (input != undefined) {
                $(input).show();
                var newName = 'U' + name;
                //添加js验证
                $(input).attr('name', newName).attr('id', newName).addClass('required');
                $(input).next('span').attr('id', newName + "-error");
            }
        } else {
            if (input != undefined) {
                $(input).next().hide();
                $(input).removeClass('required').hide();
            }
        }
    });
}
//获取字体
function getFont() {
    var url = "handler.ashx?operate=GetFont&siteId=" + siteId;
    var data = {};
    $.get(url, data, function (ajaxMsg) {
        msgBox.showMsg(ajaxMsg);
        if (ajaxMsg.status === 1) {
            var data = ajaxMsg.data;
            if (data != undefined && data.length > 0) {
                data = data[0];
                selectedOption($('#content select[name="gf1"]'), data.gf1, data.ugf1);
                selectedOption($('#content select[name="gf2"]'), data.gf2, data.ugf2);
                selectedOption($('#content select[name="gf3"]'), data.gf3, data.ugf3);
                selectedOption($('#content select[name="hf1"]'), data.hf1, data.uhf1);
                selectedOption($('#content select[name="hf2"]'), data.hf2, data.uhf2);
                selectedOption($('#content select[name="hf3"]'), data.hf3, data.uhf3);
                selectedOption($('#content select[name="navf1"]'), data.navf1, data.unavf1);
                selectedOption($('#content select[name="navf2"]'), data.navf2, data.unavf2);
                selectedOption($('#content select[name="navf3"]'), data.navf3, data.unavf3);
                                  
                selectedOption($('#content select[name="gfs"]'), data.gfs);
                selectedOption($('#content select[name="h1fs"]'), data.h1fs);
                selectedOption($('#content select[name="h2fs"]'), data.h1fs);
                selectedOption($('#content select[name="h3fs"]'), data.h1fs);
                selectedOption($('#content select[name="h4fs"]'), data.h1fs);
                selectedOption($('#content select[name="h5fs"]'), data.h1fs);
                selectedOption($('#content select[name="h6fs"]'), data.h1fs);
                selectedOption($('#content select[name="navfs"]'), data.navfs);
            }
        }
        $('#content select').select2();

    });
}
function selectedOption(select, value, uvalue) {
    $(select).find("option[value='" + value + "']").attr('selected', true);
    if (value == '') {//选择自定义的话，显示后面一个
        if (uvalue != undefined) {
            var name = $(select).attr('name');
            var newName = 'U' + name;
            var input = $(select).siblings('input');
            if (input != undefined) {
                $(input).attr('name', newName).attr('id', newName).addClass('required').val(uvalue).show();
                $(input).next('span').attr('id', newName + "-error");
            }
        }
    }
}
//获取颜色
function getColor() {
    var url = "handler.ashx?operate=GetColor&siteId=" + siteId;
    $.get(url, null, function (ajaxMsg) {
        msgBox.showMsg(ajaxMsg);
        if (ajaxMsg.status === 1) {
            var data = ajaxMsg.data;
            if (data != undefined && data.length > 0) {
                for (var i = 0; i < data.length; i++) {
                    var flag = data[i].flag;
                    var value = data[i].value;
                    var input = $('#content').find('input[name="' + flag + '"]');
                    if (input != undefined) {
                        $(input).val(value);
                        $(input).siblings('div').css('backgroundColor', value);
                    }
                }
            }
        }
    });
}
//切换颜色和字体
function show(identity) {
    $('#content').html($('#' + identity).html());
    bindChooseColor();
    if (identity === "colorDiv") {
        getColor();
    } else if (identity === "fontDiv") {
        getFont();
    }
}
//保存
function ajaxSubmit() {
    var btn = $('#content').find('input[type="submit"]');
    if (btn != undefined && btn.attr('id') != undefined) {
        if (btn.attr('id') == "saveColor") {
            msgBox.showMsgWait(txt_Loading);
            var url = 'handler.ashx?operate=SaveColor&siteId=' + siteId;
            var data = $(form).serialize();
            $.post(url, data, function (ajaxMsg) {
                msgBox.showMsg(ajaxMsg);
                $('[color]').each(function (i, e) {
                    if ($(this).val() == '') {
                        $(this).val('#FFFFFF');
                    }
                });
            });
        } else if (btn.attr('id') === "saveFont") {
            msgBox.showMsgWait(txt_Loading);
            var url = 'handler.ashx?operate=SaveFont&siteId=' + siteId;
            var data = $(form).serialize();
            $.post(url, data, function (ajaxMsg) {
                msgBox.showMsg(ajaxMsg);
            });
        }
    }
}

function setDefault(identity) {
    if (identity == 'font') {
        $('select').each(function (i, e) {
            var first = $(this).find('option:eq(0)');
            if (first != undefined)
                first.attr('selected', true);
            var input = $(this).siblings('input');
            if (input != undefined) {
                $(input).next().hide();
                $(input).removeClass('required').hide();
            }
        });
        $('#content select').select2();
    } else {
        var url = 'handler.ashx?operate=GetDefaultColor&siteId=' + siteId;
        $.get(url, null, function(ajaxMsg) {
            msgBox.showMsg(ajaxMsg);
            if (ajaxMsg.status === 1) {
                var data = ajaxMsg.data;
                if (data != undefined && data.length > 0) {
                    for (var i = 0; i < data.length; i++) {
                        var flag = data[i].flag;
                        var value = data[i].value;
                        $('[name="' + flag + '"]').val(value);
                        $('[name="' + flag + '"]').siblings('div').css('backgroundColor', value);
                    }
                }
            }
        });
    }
    msgBox.showMsgOk(txt_Success);
}

